package com.wuchao.controller;

import com.wuchao.response.Result;
import com.wuchao.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/6 12:17
 */
@Api(value = "oss管理")
@RequestMapping("/oss")
@RestController
@CrossOrigin
public class OssController {
    @Resource
    OssService ossService;
    @PostMapping("/createOssClient")
    @ApiOperation(value = "创建客户端oss")
    public Result createOssClient(){
        ossService.createOssClient();
        return  Result.ok();
    }

    @PostMapping("/createBucket")
    @ApiOperation(value = "创建存储空间oss")
    public Result createBucket(){
        ossService.createBucket();
        return  Result.ok();
    }@ApiOperation(value = "上传图片文件")
    @PostMapping("/uploadImgFile")
    public Result uploadImgFile(MultipartFile file){
        String s = ossService.upload(file);
        return Result.ok().data("url",s);
    }

    @ApiOperation(value = "删除上传替换之后的头像")
    @PostMapping("/deleteImgFile")
    public Result deleteImgFile(String file){
        //https://xinguan-parent.oss-cn-hangzhou.aliyuncs.com/2020/09/25/1575345b2cd14c13872f9b83a0aac919.png
        try {
            String[] splitFile = file.split(".com/");
            ossService.deleteFile(splitFile[1]);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }
}
